package com.android.infantschool.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.infantschool.R;
import com.android.infantschool.bean.ClockingClockingNotBean;
import com.android.infantschool.bean.ClockingClockingYesBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @particulars
 * @time 2019\6\19 0019 8:59
 * @class describe
 */
public class ClockingClockingAdapter extends RecyclerView.Adapter {

    private Context mContext;
    public List<ClockingClockingNotBean.DataBean> notBeanList = null;
    public List<ClockingClockingYesBean.DataBean> yesBeanList = null;
    private OnLister lister;

    private int VIEW_TYPE = -1;//区别列表

    public int TYPE_NOT = 2;
    public int TYPE_YES = 1;

    public ClockingClockingAdapter(Context context, OnLister lister) {
        this.mContext = context;
        this.notBeanList = new ArrayList<>();
        this.yesBeanList = new ArrayList<>();
        this.lister = lister;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        View view = null;
        RecyclerView.ViewHolder holder = null;
        if (VIEW_TYPE == TYPE_NOT) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_chocking_clocking_non_arrival, viewGroup, false);
            holder = new NotHolder(view);
        }
        if (VIEW_TYPE == TYPE_YES) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_chocking_clocking_yes_arrival, viewGroup, false);
            holder = new YesHolder(view);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
        if (VIEW_TYPE == TYPE_NOT) {
            NotHolder notHolder = (NotHolder) holder;
            ClockingClockingNotBean.DataBean notBean = (ClockingClockingNotBean.DataBean) notBeanList.get(position);
            notHolder.tvBaByName.setText(notBean.getName());
            notHolder.tvReason.setText(String.valueOf(notBean.getCause()));
            notHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (lister != null) {
                        lister.lister(position, VIEW_TYPE);
                    }
                }
            });
        }

        if (VIEW_TYPE == TYPE_YES) {
            YesHolder yesHolder = (YesHolder) holder;
            ClockingClockingYesBean.DataBean yesBean = (ClockingClockingYesBean.DataBean) yesBeanList.get(position);
            yesHolder.tvBaByName.setText(yesBean.getName());
            yesHolder.tvStartTime.setText(yesBean.getSignTime());
            yesHolder.tvTemperature.setText(yesBean.getTempture());
            yesHolder.tvEndTime.setText(yesBean.getEndTime());
            yesHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (lister != null) {
                        lister.lister(position, VIEW_TYPE);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if (VIEW_TYPE == TYPE_NOT) {
            return notBeanList != null ? notBeanList.size() : 0;
        } else if (VIEW_TYPE == TYPE_YES) {
            return yesBeanList != null ? yesBeanList.size() : 0;
        }
        return 0;
    }

    /**
     * 刷新数据
     *
     * @param list
     */
    public void setNotData(List<ClockingClockingNotBean.DataBean> list) {
        this.VIEW_TYPE = TYPE_NOT;
        if (this.notBeanList != null) {
            if (this.notBeanList.size() > 0) {
                this.notBeanList.clear();
            }
        }
        this.notBeanList.addAll(list);
        this.notifyDataSetChanged();
    }

    /**
     * 刷新数据
     *
     * @param list
     */
    public void setYesData(List<ClockingClockingYesBean.DataBean> list) {
        this.VIEW_TYPE = TYPE_YES;
        if (this.yesBeanList != null) {
            if (this.yesBeanList.size() > 0) {
                this.yesBeanList.clear();
            }
        }
        this.yesBeanList.addAll(list);
        this.notifyDataSetChanged();
    }

    /**
     * 清除数据
     */
    public void clear() {
        if (this.notBeanList != null) {
            if (this.notBeanList.size() > 0) {
                this.notBeanList.clear();
            }
        }
        if (this.yesBeanList != null) {
            if (this.yesBeanList.size() > 0) {
                this.yesBeanList.clear();
            }
        }
        this.notifyDataSetChanged();
    }

    /**
     * 未到列表
     */
    class NotHolder extends RecyclerView.ViewHolder {

        public TextView tvBaByName;
        public TextView tvReason;

        public NotHolder(@NonNull View itemView) {
            super(itemView);
            tvReason = itemView.findViewById(R.id.tv_reason);
            tvBaByName = itemView.findViewById(R.id.tv_baby_name);
        }
    }

    /**
     * 已到列表
     */
    class YesHolder extends RecyclerView.ViewHolder {

        public TextView tvBaByName;
        public TextView tvStartTime;
        public TextView tvTemperature;
        public TextView tvEndTime;

        public YesHolder(@NonNull View itemView) {
            super(itemView);
            tvBaByName = itemView.findViewById(R.id.tv_baby_name);
            tvStartTime = itemView.findViewById(R.id.tv_start_time);
            tvTemperature = itemView.findViewById(R.id.tv_temperature);
            tvEndTime = itemView.findViewById(R.id.tv_end_time);
        }
    }

    public interface OnLister {
        void lister(int position, int type);
    }
}